package player

import poker.Poker
import poker.PokerManager
import role.PokerCompareTwo
import utile.*
import kotlin.properties.Delegates

class PlayerManager {
    val Players:ArrayList<Player> = arrayListOf()
    var bottom:Int=0;
    var isSomeOneAllin:Boolean=false
    companion object{
        //单例对象
        val sharedManager:PlayerManager by lazy(
            LazyThreadSafetyMode.SYNCHRONIZED) {
            PlayerManager()
        }
    }
    init {
       for ((i,name) in PLAYER_NAME.withIndex())
       {
          Players.add(
              Player(PLAYER_NAME[i], PLAYER_MONEY[i],i)
          )

       }
    }
    fun returnExtraMoney(minBet:Int):Int{
        var totalreturn=0
        for ((i,player) in Players.withIndex())
        {
            if(player.isAlive)
            {
                totalreturn = player.lastBet-minBet
                player.money+=totalreturn
            }
        }
        return totalreturn
    }
    fun minBetMoney():Int{
        var minone  = 1000000
        for ((i,player) in Players.withIndex())
        {
            if(player.isAlive)
            {
                minone = Math.min(minone,player.lastBet)
            }
        }
        return minone
    }
    fun givePokerToEveryOne(){
        var PokerManager:PokerManager = PokerManager();
        val Pokers:ArrayList<Poker> = PokerManager.getSomePokers(4) as ArrayList<Poker>
        for ((i,player) in Players.withIndex() ){
            player.poker = Pokers[i]
        }
    }
    fun showEveryOne(){
        for ((i,player) in Players.withIndex())
        {
            println(player)
        }
    }
    fun setEveryBottomMoney(List:ArrayList<Player>,bottom:Int){
        for ((i,player) in List.withIndex())
        {
            player.money-=bottom
        }
    }
    fun findNextPlayer(start:Int):Int{
        var index = start
        while (true){
            //防止越界
            index %= Players.size
            //获取start对应的玩家
            if (Players[index].isAlive){
                "$index${Players[index].isAlive}".showWithEnter()
                return index
            }
            index++
        }
    }
    fun findMaxPlayer():Player{
        var MaxPlayer = Players[findNextPlayer(0)]
        for ((i,player) in Players.withIndex())
        {
            if(player.isAlive)
            {
                if(!PokerCompareTwo(MaxPlayer.poker,player.poker))
                {
                    MaxPlayer=player
                }
            }
        }
        return MaxPlayer
    }
    fun awardWinner(awardMoney:Int){
        val player = Players[findNextPlayer(0)]
        "${player.name}获得胜利".showWithEnter()
        player.money += awardMoney
    }
    fun awardWinner(player:Player,awardMoney: Int){
        "${player.name}获得胜利".showWithEnter()
        player.money+=awardMoney
    }
    fun showNowPersonTable(index:Int):Int{
        showSTATRSLINE()
        println(Players[index])
        "请选择操作:".show()
        val choice= setChoice()
        showSTATRSLINE()
        return choice
    }
    fun showALLpeopleInfomation(){
        showSTATRSLINE()
        for ((i,player) in Players.withIndex())
        {
            print(i )
            println(player)
        }
        showSTATRSLINE()
    }
    fun showALLpeopleInfomationWithPoker(){
        showSTATRSLINE()
        for ((i,player) in Players.withIndex())
        {
            if(player.isAlive) {
                println(player)
                println(player.poker)
            }
        }
        showSTATRSLINE()
    }

    fun isMoneyEnough(playerIndex:Int,money:Int):Boolean{
        return Players[playerIndex].money >= money
    }
    fun showCompareTwoPoker(p1:Player,p2:Player){
        "请发牌员展示${p1.name}和${p2.name}的牌".showWithEnter()
        showPlayerPoker(p1)
        showPlayerPoker(p2)

    }

}